import 'package:flutter/material.dart';
import 'package:flutter_news/common/utils/utils.dart';
import 'package:flutter_news/common/values/values.dart';

// 关闭软键盘
void closeKeyboard(BuildContext context) {
  FocusScope.of(context).requestFocus(FocusNode());
}

/// 输入相关的控件
Widget inputTextEdit({
  required TextEditingController controller,
  TextInputType keyboardType = TextInputType.text,
  String? hintText,
  bool isPassword = false,
  int maxLen = 2000,
  FocusNode? focusNode = null,
}) {
  InputBorder normalBorder = OutlineInputBorder(
    // 边框圆角
    borderRadius: Radii.k6pxRadius,

    borderSide: BorderSide(
      // 边框颜色
      color: Colors.transparent,
      // 边框的粗细
      width: duSetWidth(1),
    ),
  );

  // InputBorder normalBorder = InputBorder.none;

  InputBorder focusedBorder = OutlineInputBorder(
    borderRadius: Radii.k6pxRadius,
    borderSide: BorderSide(
      color: Colors.transparent,
      width: duSetWidth(1),
    ),
  );

  return SizedBox(
    width: duSetWidth(295),
    height: duSetHeight(44),
    child: TextField(
      controller: controller,
      // 设置键盘类型
      keyboardType: keyboardType,
      // 设置最大的行数
      maxLines: 1,
      // 设置最大的输入数
      maxLength: maxLen,
      // 自动纠正
      autocorrect: false,
      // 因此输入的内容，密码框
      obscureText: isPassword,

      focusNode: focusNode, // 焦点控制

      decoration: InputDecoration(
        // 设置内容间距
        contentPadding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        // 设置背景填充颜色
        fillColor: AppColors.secondaryElement,
        // true 的时候，fillColor 才生效
        filled: true,
        // 不可用时候的边框样式
        disabledBorder: normalBorder,
        // 普通状态的边框样式
        border: normalBorder,
        // 可用的边框样式
        enabledBorder: normalBorder,
        // 获取焦点时候的边框样式
        focusedBorder: focusedBorder,
        // 未输入内容时候的提示文本
        hintText: hintText,
        // 未输入内容时候的提示文本的样式
        hintStyle: TextStyle(
          color: AppColors.gray,
          fontFamily: FontFamily.avenir,
          fontWeight: FontWeight.w400,
          fontSize: duSetFontSize(14),
        ),
      ),

      // 内容样式
      style: TextStyle(
        color: AppColors.primaryText,
        fontFamily: FontFamily.avenir,
        fontWeight: FontWeight.w400,
        fontSize: duSetFontSize(18),
      ),

      // 设置了 maxLength 之后，右下角会有计算输入字符的视图，
      // 要想隐藏这个视图，传递 buildCounter ，并且返回 null 即可
      buildCounter: (
        BuildContext context, {
        required int currentLength,
        required int? maxLength,
        required bool isFocused,
      }) {
        return null;
      },
    ),
  );
}

/// email 输入框
/// 背景白色，文字黑色，带阴影
Widget inputEmailEdit({
  TextEditingController? controller,
  TextInputType keyboardType = TextInputType.text,
  String? hintText,
  bool isPassword = false,
  double marginTop = 15,
  bool autofocus = false,
}) {
  return Container(
    height: duSetHeight(44),
    margin: EdgeInsets.only(top: duSetHeight(marginTop)),
    decoration: BoxDecoration(
      color: AppColors.primaryBackground,
      borderRadius: Radii.k6pxRadius,
      boxShadow: [
        BoxShadow(
          color: Color.fromARGB(41, 0, 0, 0),
          offset: Offset(0, 1),
          blurRadius: 0,
        ),
      ],
    ),
    child: TextField(
      autofocus: autofocus,
      controller: controller,
      keyboardType: keyboardType,
      decoration: InputDecoration(
        hintText: hintText,
        contentPadding: EdgeInsets.fromLTRB(20, 10, 0, 9),
        border: InputBorder.none,
        hintStyle: TextStyle(
          color: AppColors.primaryText,
        ),
      ),
      style: TextStyle(
        color: AppColors.primaryText,
        fontFamily: "Avenir",
        fontWeight: FontWeight.w400,
        fontSize: duSetFontSize(18),
      ),
      maxLines: 1,
      autocorrect: false,
      // 自动纠正
      obscureText: isPassword, // 隐藏输入内容, 密码框
    ),
  );
}
